﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;

namespace GET_RPO_and_IBS_Import
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var path = path2savePRO.Text;

                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                
                var IbsConnectionString = Properties.Settings.Default.IbsConnectionString;
                
                var ibs = new IBS.IbsDataContext(IbsConnectionString);

                var SaleOrderList = ibs.GetSaleOrdersTNK();

                foreach (var saleOrder in SaleOrderList)
                {
                    try
                    {
                        var RPO_data = ibs.GetRPO(saleOrder.INVOICE_ID);
                        foreach (var line in RPO_data)
                        {                            
                            if (!Directory.Exists(path + @"\" + line.IP))
                                Directory.CreateDirectory(path + @"\" + line.IP);
                            line.SerializeToFile(path +@"\" + line.IP + @"\RPO_Invoice_" + line.LOCATION_KEY + "_" + line.SUPPLIERCODE + "_" + line.INVOICECODE + "_" + line.INVOICEDATE.ToString("ddMMyyyy") + ".1");
                        }
                        ibs.UpdateStatus(saleOrder.INVOICE_ID);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message,"Ошибка",MessageBoxButton.OK,MessageBoxImage.Error,MessageBoxResult.None);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.None);

            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var path = path2saveIBSImportFile.Text;

                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                var IbsConnectionString = Properties.Settings.Default.IbsConnectionString;

                var ibs = new IBS.IbsDataContext(IbsConnectionString);



                var SaleOrderList = ibs.GetSaleOrdersBP();

                foreach (var saleOrder in SaleOrderList)
                {
                    try
                    {
                        var ibsImportData = ibs.GetIBSImport(saleOrder.INVOICE_ID);
                        foreach (var line in ibsImportData)
                        {
                            if (!Directory.Exists(path + @"\" + line.SITEIBSCODE))
                                Directory.CreateDirectory(path + @"\" + line.SITEIBSCODE);
                            line.SerializeToFile(path + @"\" + line.SITEIBSCODE + @"\" + line.COMPANY_KEY + "_" + line.DOC_NUMBER + "_" + line.DOC_DATE.ToString("yyyy.MM.dd") + ".txt");
                        }
                        ibs.UpdateStatus(saleOrder.INVOICE_ID);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.None);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.None);

            }
        }
    }
}
